const tool = require('../../tool');

module.exports = function handleGenerateSearch(tableConfig) {
  //引入组件部分 目前先全部引入，如果影响性能再在生成后手动把它删掉，
  //后期可以配置成，按需引入的形式
  //使用组件部分
  var tSearchTpl = '';  //t是table
  var searchBtnTpl = "";
  var searchMethodFirst = '';
  var searchMethodSecond = '';
  var formDataContent = `
    formData: {
  `; //formData数据
  var selectOptsContent = ''; //selectOpts数据
  var dateMethodsContent = ''; //date方法的数据
  var addBtnTpl = '';
  var addBtnInSearchMethodFirst = "";
  var addBtnInSearchMethodSecond = "";

  var searchConfig;

  for (var item in tableConfig) {

    if (item == 'search') {
      //每个页面要单独清空信息
      selectOptsContent = '';
      dateMethodsContent = '';
      addBtnTpl = '';
      addBtnInSearchMethodFirst = "";
      addBtnInSearchMethodSecond = "";

      searchConfig = tableConfig[item];
      var contentArr = tableConfig[item].contentArr;
      for (var searchName in contentArr) {
        var searchItemObj = contentArr[searchName];
        if (searchName.includes('input')) {
          // console.log('input');
          tSearchTpl += getInputSearchTpl({
            labelName: searchItemObj.labelName,
            inputName: searchItemObj.inputName
          });
          //数据
          formDataContent += `
            ${searchItemObj.inputName}: "", `;

        } else if (searchName.includes('select')) {
          // console.log('select');
          tSearchTpl += getSelectSearchTpl({
            labelName: searchItemObj.labelName,
            inputName: searchItemObj.inputName,
            selectOptsName: searchItemObj.selectOptsName,
          });
          formDataContent += `
            ${searchItemObj.inputName}: "", `;
          selectOptsContent += `
            ${searchItemObj.selectOptsName}: ${searchItemObj.selectOptsArr}`;

        } else if (searchName.includes('date')) {
          // console.log('date');
          var dateChangeMethodsName =
            searchItemObj.dateChangeMethodsName
              ? searchItemObj.dateChangeMethodsName
              : 'handleDateChange';
          tSearchTpl += dateSearchTpl({
            labelName: searchItemObj.labelName,
            dateChangeMethodsName: searchItemObj.dateConfig.dateChangeMethodsName, //日期的作用描述，用于区分多个日期
          });
          formDataContent += `
            ${searchItemObj.dateConfig.startDateName}: "",
            ${searchItemObj.dateConfig.endDateName}: "",
            `;
          dateMethodsContent += `
            //日期选择
            ${searchItemObj.dateConfig.dateChangeMethodsName} (dateArr, startDate, endDate) {
              this.formData.${searchItemObj.dateConfig.startDateName} = startDate;
              this.formData.${searchItemObj.dateConfig.endDateName} = endDate;
            },
          `;
        }
      }

    }
  }

  //添加formData对象闭合符号
  formDataContent += `
    },
  `;

  //添加额外方法的data数据
  var extraDataStr = ``;
  for (var itemKey in tableConfig.extraRequest) {
    var extraItem = tableConfig.extraRequest[itemKey];
    if (itemKey == 'methodsCode') { //进入列表方法配置
      extraItem.forEach((item, index) => {
        if (item.type == "opts") {
          extraDataStr += `
            ${item.optsName}: [],
          `;
        } else if (item.type.includes('opts') && item.type.includes('shows')) {
          extraDataStr += `
          ${item.optsName}: [],
          ${item.showName}: {},
        `;
        }
      });
    }
  }

  //输入数据的方法
  var inputDataContent = formDataContent + selectOptsContent + extraDataStr;


  var searchBtnConfig = searchConfig.searchBtn;
  //查找方法
  if (searchConfig.searchBtn) {
    searchBtnTpl = getSearchBtnCon({
      text: searchBtnConfig.text
    });
    searchMethodFirst = getSearchMethodsFirst({
      text: searchBtnConfig.text
    });
    searchMethodSecond = getSearchMethodsSecond(searchConfig, {
      text: searchBtnConfig.text
    });
  }


  //添加方法
  if (searchConfig.addBtn) {
    var addBtnConfig = searchConfig.addBtn;
    addBtnTpl = getAddBtnCon({
      text: addBtnConfig.text,
    });
    addBtnInSearchMethodFirst = getAddBtnMethodFirst({
      text: addBtnConfig.text,
    });
    addBtnInSearchMethodSecond = getAddBtnMethodSecond({
      text: addBtnConfig.text, //文本
      path: addBtnConfig.jump,
      addBtnConfig,
      extraQueryParams: addBtnConfig.extraQueryParams,
    });
  }

  var searchBtnGroup = searchBtnTpl + addBtnTpl;
  tSearchTpl = getSearchContainerTpl(tSearchTpl, searchBtnGroup);

  return {
    searchConTpl: tSearchTpl, //输入组件模板
    inputDataContent,  //输入的data数据 拼接于data中
    dateMethodsContent,  //用于组件的函数方法 如果没有日期组件则为空
    // searchBtnTpl,
    searchMethodFirst, //查找相关方法 查找和添加
    searchMethodSecond, //查找相关方法2
    // addBtnTpl,
    addBtnInSearchMethodFirst,
    addBtnInSearchMethodSecond,
  };
}

//添加按钮
function getAddBtnCon(opt) {
  var text = opt.text;
  return `<el-button @click="handleAdd" >${text}</el-button>
  `;
}

//添加方法1
function getAddBtnMethodFirst(opt) {
  var text = opt.text;
  return `//${text}
  handleAdd(e) {
    this.allMethods.handleAdd(e);
  },
  `;
}

//添加方法2
function getAddBtnMethodSecond(opt) {
  const path = opt.path,
    text = opt.text,
    extraQueryParams = opt.extraQueryParams ? opt.extraQueryParams : "{}";
  return `//${text}
    handleAdd (e) {
      _this.$router.push({
        path: '${path}',
        query: ${extraQueryParams},
      })
  },
  `;
}

//查询按钮
function getSearchBtnCon(opt) {
  var text = opt.text;
  return `<el-button @click="handleSearch" class="orange">${text}</el-button>
  `;
}

// 组装查找的方法
function getSearchMethodsFirst(opt) {
  var text = opt.text;
  return `
    //${text}
    handleSearch() {
      this.allMethods.handleSearch();
    },
  `;
}

// 查找方法的
function getSearchMethodsSecond(searchConfig, opt) {
  // console.log(searchConfig);
  var paramsDataStr = "";
  for (var item in searchConfig.contentArr) {
    var curSearchItem = searchConfig.contentArr[item];
    if (!item.includes('date')) {
      paramsDataStr += `
      ${curSearchItem.inputName},`;
    } else {
      paramsDataStr += `
        ${curSearchItem.dateConfig.startDateName},
        ${curSearchItem.dateConfig.endDateName},
      `;
    }
  }
  

  var text = opt.text,
  extraParams = searchConfig.searchBtn.extraParams  //查询的额外参数
              ? searchConfig.searchBtn.extraParams : "";
  // console.log(paramsDataStr);
  return `
    //${text}
    handleSearch() {
      var {
       ${paramsDataStr}
      } = _this.formData;
      _this.LingChuang_request.handleSearch({
       ${paramsDataStr}
        pageIndex: _this.currentPageIdx,
        pageSize: _this.onePageSize,
        ${extraParams}
      });
    },
  `
}

//查找的容器
function getSearchContainerTpl(totalInputConStr, searchBtnGroup) {
  return `
    <top-option>
    <div slot="left-box">
      ${totalInputConStr}
    </div>
    <div slot="right-box">
      <div class="sub">
        ${searchBtnGroup}
      </div>
    </div>
  </top-option>
  `;
};

//所有组件展示
/* 
<input-search
  lableName="发起人手机号"
  inputValue="userTel"
  :tThis="this"
  inputPlaceholder="请输入发起人手机号"
/>
<select-search
  lableName="活动状态"
  selectIdName="autoState"
  :tThis="this"
  selectPlaceholder="请选择活动状态"
  :selectOpts="autoStateOpts"
  selectWidth="150px;"
  labelWidth="60px;"
/>
<date-search
  lableName="活动创建时间"
  :tThis="this"
  labelWidth="100px;"
  dateWidth="120px;"
  @dateChage="handleActivityCreateDateChange"
/> 

*/

//输入
function getInputSearchTpl(opt) {
  var labelName = opt.labelName ? opt.labelName : '标签', //发起人手机号
    inputName = opt.inputName ? opt.inputName : 'inputValue';  //userTel
  return `
    <input-search
      lableName="${labelName}"
      inputValue="${inputName}"
      :tThis="this"
      inputPlaceholder="请输入${labelName}"
    />
  `
}

//选择
function getSelectSearchTpl(opt) {
  var labelName = opt.labelName ? opt.labelName : '标签', //活动状态
    inputName = opt.inputName ? opt.inputName : 'selectValue',  //userTel
    selectOptsName = opt.selectOptsName ? opt.selectOptsName : 'defaultOpts';
  return `
    <select-search
      lableName="${labelName}"
      selectIdName="${inputName}"
      :tThis="this"
      selectPlaceholder="请选择${labelName}"
      :selectOpts="${selectOptsName}"
      selectWidth="150px;"
      labelWidth="80px;"
    />
    `;
}

//日期
function dateSearchTpl(opt) {
  var labelName = opt.labelName ? opt.labelName : '活动创建时间', //发起人手机号
    dateChangeMethodsName = opt.dateChangeMethodsName ? opt.dateChangeMethodsName : 'handleDateChange';  //userTel
  return `
    <date-search
      lableName="${labelName}"
      :tThis="this"
      labelWidth="80px;"
      dateWidth="150px;"
      @dateChange="${dateChangeMethodsName}"
    /> 
  `
}